import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../controller/build_controller.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key});

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  // Map data = {
  //   "point": "", // 记录小区信息
  //   "size": 0, // 随机产生楼栋的数量
  //   "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  // };
  // 楼栋的数量
  int _size = 0;

  /// 引入楼栋控制器
  BuildController buildController = Get.find<BuildController>();
// 初始化楼栋数量
  @override
  void initState() {
    super.initState();
    // 1. 获取小区的楼栋数量
    // 1. 产生一个随机数(2-10)
    Random random = Random();
    _size = random.nextInt(9) + 2;
    setState(() {});
  }

  /// 如果size>5 则显示单元, 否则显示楼栋
  Widget getShowText(int index) {
    if (_size > 5) {
      return Text('${buildController.buildInfo['point']}$index单元');
    } else {
      return Text('${buildController.buildInfo['point']}$index栋');
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BuildController>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('选择楼栋'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
                onTap: () {
                  // 1. 更新全局数据
                  buildController.updateBuildInfo({
                    'point': '${buildController.buildInfo['point']}',
                    'building': _size > 5 ? '${index + 1}单元' : '${index + 1}栋',
                  });
                  Navigator.pushNamed(context, '/room_list');
                },
                child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Row(
                    children: [
                      Expanded(child: getShowText(index + 1)),
                      const Row(
                        children: [
                          Icon(Icons.arrow_forward_ios,
                              size: 16, color: Colors.black),
                        ],
                      )
                    ],
                  ),
                ));
            // return Container(
            //   color: Colors.white,
            //   padding: const EdgeInsets.all(10),
            //   child: Row(
            //     children: [
            //       // Expanded(
            //       //   child: Text('${buildController.buildInfo['point']}'),
            //       // ),
            //       Expanded(child: getShowText(index + 1)),
            //       const Row(
            //         children: [
            //           Icon(Icons.arrow_forward_ios,
            //               size: 16, color: Colors.black),
            //         ],
            //       )
            //     ],
            //   ),
            // );
          },
          itemCount: _size,
        ),
      );
    });
  }
}
